package ua.pp.parkee.t4;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by parkee on 20.08.14.
 */
public class LockFreeBigInteger {

    private AtomicReference<BigInteger> bigInteger;

    public LockFreeBigInteger() {
        bigInteger = new AtomicReference<>(new BigInteger("1"));
    }

    public BigInteger next() {

        BigInteger current;
        BigInteger next;

        do {
            current = bigInteger.get();
            next = bigInteger.get().multiply(BigInteger.valueOf(2));
        } while (!bigInteger.compareAndSet(current, next));

        return next;
    }
}
